package main

import "fmt"

// 判断v是否实现接口i
// 返回值ok为true 表示已实现，ok为false 表示未实现

// if _,ok :=v.(i);ok{
// 	// do something
// }

type geometry interface {
	area() float64
	perimeter() float64
}

type rectangle struct {
	width, height float64
}

type circle struct {
	radius float64
}

func (r *rectangle) area() float64 {
	return r.width * r.height
}

func (r *rectangle) perimeter() float64 {
	return 2 * (r.width + r.height)
}

func main() {
	var r interface{}

	r = &rectangle{width: 3, height: 4}

	if v, ok := r.(geometry); ok {
		fmt.Printf("r implements interface geometry ,area = %.2f, perimeter = %.2f\n", v.area(), v.perimeter())
	}

	var c interface{}
	c = &circle{radius: 5}
	if v, ok := c.(geometry); ok {
		fmt.Printf("c implements interface geometry ,area = %.2f, perimeter = %.2f\n", v.area(), v.perimeter())
	} else {
		fmt.Println("c not implements interface geometry")
	}
}
